package com.example.jobdemo.controller;

import com.example.jobdemo.entity.User;
import com.example.jobdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @Author Admin
 * @Description
 * @Date 2021-06-28 下午8:12
 **/

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    JavaMailSenderImpl javaMailSender;

    @RequestMapping("register")
    public Map<String, Object> register(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        int counta = userMapper.selectUserByAccount(user.getUserName(), null);
        if (counta != 0) {
            map.put("code", 500);
            map.put("msg", "用户名已经存在");
            return map;
        }
        int counte = userMapper.selectUserByEmail(user.getUserEmail(), null);
        if (counte != 0) {
            map.put("code", 500);
            map.put("msg", "邮箱已经存在");
            return map;
        }
        user.setUserAccount(user.getUserName());
        user.setUserPower("2");
        user.setProtect(false);
        user.setFrozen(false);
        user.setCreateTime(new Date());
        userMapper.insert(user);
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }

    @RequestMapping("login")
    public Map<String, Object> login(@RequestBody User user) {
        User loginUser = userMapper.login(user);
        Map<String, Object> map = new HashMap<>();
        if (loginUser == null) {
            map.put("code", 500);
            map.put("msg", "账号密码不正确");
            return map;
        } else if (loginUser.isFrozen()){
            map.put("code", 500);
            map.put("msg", "账号被冻结");
            return map;
        } else {
            map.put("code", 200);
            map.put("msg", "成功");
            map.put("user", loginUser);
        }
        return map;
    }

    @RequestMapping("update")
    public Map<String, Object> update(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        int counta = userMapper.selectUserByAccount(user.getUserName(), user.getUserId());
        if (counta != 0) {
            map.put("code", 500);
            map.put("msg", "用户名已经存在");
            return map;
        }
        int counte = userMapper.selectUserByEmail(user.getUserEmail(), user.getUserId());
        if (counte != 0) {
            map.put("code", 500);
            map.put("msg", "邮箱已经存在");
            return map;
        }
        user.setUpdateTime(new Date());
        userMapper.updateByPrimaryKeySelective(user);
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }

    @RequestMapping("page")
    public Map<String, Object> page(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        Integer count = userMapper.selectUserCount(user.getUserName());
        int pageIndex = (user.getPageIndex() - 1) * user.getPageSize();
        List<User> userList = userMapper.selectUserList(user.getUserName(), pageIndex, user.getPageSize());
        map.put("code", 200);
        map.put("msg", "成功");
        map.put("data", userList);
        map.put("count", count);
        return map;
    }

    @RequestMapping("updatePassWord")
    public Map<String, Object> updatePassWord(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        User old = userMapper.selectByPrimaryKey(user.getUserId());
        if (!user.getOldPassword().equals(old.getUserPassword())){
            map.put("code", 200);
            map.put("msg", "旧密码不正确");
            return map;
        }
        old.setUserPassword(user.getUserPassword());
        old.setUpdateTime(new Date());
        userMapper.updateByPrimaryKeySelective(old);
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }

    @RequestMapping("forgetPassWord")
    public Map<String, Object> forgetPassWord(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        User old = userMapper.selectByEmail(user.getUserEmail());
        if (old==null){
            map.put("code", 500);
            map.put("msg", "邮箱未被注册");
            return map;
        }
        old.setUserPassword(user.getUserPassword());
        old.setUpdateTime(new Date());
        userMapper.updateByPrimaryKeySelective(old);
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }

    @RequestMapping("delete")
    public Map<String, Object> delete(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        userMapper.deleteByPrimaryKey(user.getUserId());
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }

    @RequestMapping("sendEmail")
    public Map<String, Object> sendEmail(@RequestBody User user) {
        SimpleMailMessage message = new SimpleMailMessage();
        int code = (int)((Math.random() * 9 + 1) * 100000);
        //邮件设置
        message.setSubject("基于信息安全的求职分析平台");
        message.setText("【基于信息安全的求职分析平台】 验证码："+code+"(有效期3分钟)，请勿泄露给他人，如非本人操作，请忽略！");
        message.setTo(user.getUserEmail());
        message.setFrom("553606789@qq.com");
        javaMailSender.send(message);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        map.put("data",code);
        return map;
    }

    @RequestMapping("/fileUpload")
    public String fileUpload(@RequestParam(value = "file") MultipartFile file,@RequestParam("userId") Long userId) {
        if (file.isEmpty()) {
            System.out.println("文件为空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = "D://img//"; // 上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        User user = new User();
        user.setUserId(userId);
        String filename = "/img/" + fileName;
        user.setUserImg(filename);
        userMapper.updateByPrimaryKeySelective(user);
        return filename;
    }
}
